package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.situ.mall.entity.Cart;
import com.situ.mall.mapper.CartMapper;
import com.situ.mall.service.ICartService;

@Service
public class CartServiceImpl implements ICartService{
	@Autowired
	private CartMapper cartMapper;

	@Override
	public boolean insert(Cart cart) {
		// 首先判断这个商品是否已经在当前用户的购物车中了
		Cart c = cartMapper.selectByUserIdAndProductId(cart.getUserId(), cart.getProductId());
		int count = 0;
		// 1、已经存在，更新一下quantity数量就可以
		if (c != null) {
			count = cartMapper.updateQuantity(c.getId(), cart.getQuantity());
		} else {
			// 2、如果不存在，就插入一条新的
			count = cartMapper.insert(cart);
		}
		return count == 1 ? true : false;
	}

	@Override
	public List<Cart> selectByUserId(Integer id) {
		return cartMapper.selectByUserId(id);
	}

	@Override
	public boolean deleteById(Integer cartId) {
		int count = cartMapper.deleteByPrimaryKey(cartId);
		return count == 1 ? true : false;
	}

	@Override
	public boolean updateQuantity(Integer cartId, Integer num) {
		int count = cartMapper.updateQuantity(cartId, num);
		return count == 1 ? true : false;
	}

	@Override
	public boolean updateCheckStatus(Integer cartId, Integer checked) {
		int count = cartMapper.updateCheckStatus(cartId, checked);
		return count == 1 ? true : false;
	}

	@Override
	public List<Cart> selectByUserIdAndChecked(Integer id) {
		return cartMapper.selectByUserIdAndChecked(id);
	}

}
